package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhAdvanceIndemnityMapper;
import com.zjdiepu.www.ibiz.dh.IDhAdvanceIndemnityService;
import com.zjdiepu.www.model.dh.dto.DhAdvanceIndemnity;
import com.zjdiepu.www.model.dh.ext.DhAdvanceIndemnityExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月21日 09时30分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhAdvanceIndemnityServiceImpl extends BaseService implements IDhAdvanceIndemnityService {
	public static final Logger logger = LoggerFactory.getLogger(DhAdvanceIndemnityServiceImpl.class);

	@Autowired
	private DhAdvanceIndemnityMapper dhAdvanceIndemnityMapper;
	
	@Override
	public void saveUpdateAndDelete(DhAdvanceIndemnity dhAdvanceIndemnity, OperateType operType) {
		switch (operType) {
		case add:
			dhAdvanceIndemnity.initInsertData(dhAdvanceIndemnity.getModifier(), dhAdvanceIndemnity.getModifierName());
			dhAdvanceIndemnityMapper.insert(dhAdvanceIndemnity);
			break;
		case edit:
			dhAdvanceIndemnity.initUpdateData(dhAdvanceIndemnity.getModifier(), dhAdvanceIndemnity.getModifierName());
			// 影响的记录数
			int updateCount = dhAdvanceIndemnityMapper.updateByPrimaryKeySelective(dhAdvanceIndemnity);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhAdvanceIndemnity.initDeleteData(dhAdvanceIndemnity.getModifier(), dhAdvanceIndemnity.getModifierName());
			// 影响的记录数
			int deleteCount = dhAdvanceIndemnityMapper.updateByPrimaryKeySelective(dhAdvanceIndemnity);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhAdvanceIndemnity> selectByExt(DhAdvanceIndemnityExt dhAdvanceIndemnityExt) {
		return dhAdvanceIndemnityMapper.selectByExt(dhAdvanceIndemnityExt);
	}
	
	@Override
	public int countByExt(DhAdvanceIndemnityExt dhAdvanceIndemnityExt){
		return dhAdvanceIndemnityMapper.countByExt(dhAdvanceIndemnityExt);
	}
	
	@Override
	public DhAdvanceIndemnity selectById(String id) {
		return dhAdvanceIndemnityMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhAdvanceIndemnityMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhAdvanceIndemnity dhAdvanceIndemnity) {
		return dhAdvanceIndemnityMapper.countRefined(dhAdvanceIndemnity);
	}

	@Override
	public DhAdvanceIndemnity selectRefined(DhAdvanceIndemnity dhAdvanceIndemnity) {
		return dhAdvanceIndemnityMapper.selectRefined(dhAdvanceIndemnity);
	}

	@Override
	public boolean repeat(DhAdvanceIndemnity dhAdvanceIndemnity) {
		int count = countRefined(dhAdvanceIndemnity);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhAdvanceIndemnity.getId())) {
			return true;
		}
		DhAdvanceIndemnity dbItem = selectRefined(dhAdvanceIndemnity);
		if(null == dbItem) {
			return false;
		}
		return !dhAdvanceIndemnity.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhAdvanceIndemnityMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
